package utils


import (
	"fmt"
	"github.com/sony/sonyflake"
	"time"
)

var (
	sonyFlake     *sonyflake.Sonyflake
	sonyMachineID uint16
)

func InitIdGen(machineID uint16) {
	sonyMachineID = machineID
	settings := sonyflake.Settings{
		StartTime:      time.Time{},
		MachineID:      nil,
		CheckMachineID: nil,
	}
	settings.MachineID = getMachineID
	sonyFlake = sonyflake.NewSonyflake(settings)
}

func GetId() (id uint64, err error) {
	if sonyFlake == nil {
		err = fmt.Errorf("sonyFlake is not init")
		return
	}
	id, err = sonyFlake.NextID()
	return
}

func getMachineID() (machineID uint16, err error) {
	machineID = sonyMachineID
	return
}
